package com.cmict.data.form.api.util.SubUtil;

import cn.hutool.core.util.ObjectUtil;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.form.api.entity.vo.SysDatasourceVO;
import com.cmict.data.form.api.entity.vo.SysFromDataColumnVo;
import com.cmict.data.form.api.service.SysCurrencyService;
import com.cmict.data.system.api.dict.DictDataApi;
import com.cmict.data.system.api.dict.dto.DictDataDTO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Set;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

/**
 * @description: el-input组件规则
 * @author: qiaoyu
 * @create: 2022-07-18 22:57
 **/
public class ElSelectUtil {

	public static CommonResult rule (JSONObject personData , String value , int number,
									 DictDataApi dictDataService, SysCurrencyService sysCurrencyService) {
		String message = "";
		Boolean redata = true;
		JSONArray fields = personData.getJSONArray("fields");
		JSONObject field = fields.getJSONObject(number);
		JSONObject config = field.getJSONObject("__config__");
		String label = config.getString("label");
		//是否必填
		Boolean required = config.getBoolean("required");
		if(required && ObjectUtil.isEmpty(value)){
			message = label+"是必填项;";
			redata=false;
			//return success( label+"是必填项",false);
		}
		String datatype = field.getString("dataType");
		if("static".equals(datatype)){
			Boolean select = false;
			JSONArray optionsList = field.getJSONArray("options");
			String mas = "";
			for (int i = 0; i <optionsList.size() ; i++) {
				JSONObject options = optionsList.getJSONObject(i);
				if(options.get("label").equals(value)){
					select = true;
				}
				mas = mas +","+options.get("label");
			}
			message = message + label+"请在"+mas+"中选择数据"+";";
			redata=select;
		}
		if("dictionary".equals(datatype)){
			Boolean select = false;
			String dictName = field.getString("dictName");
			List<DictDataDTO> dictList =  dictDataService.getListByDictType(dictName).getCheckedData();
			String mas = "";
			for (int i = 0; i <dictList.size() ; i++) {
				String labelName = dictList.get(i).getLabel();
				if(labelName.equals(value)){
					select = true;
				}
				mas = mas +","+labelName;
			}
			message = message + label+"请在"+mas+"中选择数据;";
			redata=select;
		}
		if("datasource".equals(datatype)){
			Boolean select = false;
			String tableName = field.getString("tableName");
			String fieldName = field.getString("fieldName");
			SysDatasourceVO sysDatasourceVO = new SysDatasourceVO();
			//sysDatasourceVO.setTableSchema("awesometables");
			sysDatasourceVO.setTableName(tableName);
			sysDatasourceVO.setColumn(fieldName);
			Set<String> list = sysCurrencyService.getData(sysDatasourceVO);
			String mas = "";
			for (String labelName : list) {
				if(labelName.equals(value)){
					select = true;
				}
				mas = mas +","+labelName;
			}
			message = message + label+"请在"+mas+"中选择数据;";
			redata=select;
		}
		if(!redata){
			return success( message,false);
		}
		return success( "通过",true);
	}

	public static SysFromDataColumnVo State (JSONObject personData , String value , int number , SysFromDataColumnVo vo) {
		JSONArray fields = personData.getJSONArray("fields");
		JSONObject field = fields.getJSONObject(number);
		String datatype = field.getString("dataType");
		if("static".equals(datatype)){
			JSONArray optionsList = field.getJSONArray("options");
			for (int i = 0; i <optionsList.size() ; i++) {
				JSONObject options = optionsList.getJSONObject(i);
				if(options.get("label").equals(value)){
					vo.setValue(String.valueOf(options.get("value")));
				}
			}
		}
		return vo;
	}
}

